package pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.gui

import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.dungeon.floor.DungeonFloor
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.dungeon.floor.catacombs.{FloorCatacombs, FloorMasterCatacombs}
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.gui.MenuGui.instance
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.gui.tab.DungeonTypeTab
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.manager.ConfigManager

import java.awt.BorderLayout
import java.awt.event.WindowAdapter
import java.util
import javax.swing._

class MenuGui extends JFrame{
  private val container = getContentPane
  private val commandText = new JTextField(ConfigManager.Config.fastdungeon)
  private val tab = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT)

  private val catacombsTab = new DungeonTypeTabImpl(new util.ArrayList[DungeonFloor](util.Arrays.asList(FloorCatacombs.FLOOR_0,FloorCatacombs.FLOOR_1,FloorCatacombs.FLOOR_2,FloorCatacombs.FLOOR_3,FloorCatacombs.FLOOR_4,FloorCatacombs.FLOOR_5,FloorCatacombs.FLOOR_6,FloorCatacombs.FLOOR_7)))
  private val masterCatacombsTab = new DungeonTypeTabImpl(new util.ArrayList[DungeonFloor](util.Arrays.asList(FloorMasterCatacombs.FLOOR_1,FloorMasterCatacombs.FLOOR_2,FloorMasterCatacombs.FLOOR_3,FloorMasterCatacombs.FLOOR_4,FloorMasterCatacombs.FLOOR_5,FloorMasterCatacombs.FLOOR_6,FloorMasterCatacombs.FLOOR_7)))

  initFrame()
  initComps()
  addListener()

  private def initFrame(): Unit = {
    setTitle(ConfigManager.Messages.menugui_title)
    setLayout(new BorderLayout())
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
    setSize(500, 350)
    setLocationRelativeTo(null)
  }
  private def initComps(): Unit = {
    tab.addTab("Catacombs", catacombsTab)
    tab.addTab("Master Catacombs", masterCatacombsTab);

    container.add(tab)
    commandText.setEditable(false)
    container.add(commandText, BorderLayout.SOUTH)
  }
  private def addListener(): Unit = {
    addWindowListener(new WindowAdapter {
      override def windowClosing(e : java.awt.event.WindowEvent) {
        instance = null
        dispose()
      }
    })
  }

  class DungeonTypeTabImpl(override val data : java.util.List[DungeonFloor]) extends DungeonTypeTab(data){
    override def onSelect(selected: DungeonFloor): Unit = {
      commandText.setText(if (selected == null) "" else selected.cmd)
    }
  }
}
object MenuGui {
  private var instance : MenuGui = _
  def getInstance(): MenuGui = instance
  def create(): MenuGui = {
    if (instance == null) {
      instance = new MenuGui
    }
    instance.setVisible(true)

    instance
  }
}
